//-----------------------------------------------------------------------------
//
//  <No description>
//
//  Microsoft dotNetMF Project
//  Copyright ©2001,2002,2003,2004 Microsoft Corporation
//  One Microsoft Way, Redmond, Washington 98052-6399 U.S.A.
//  All rights reserved.
//  MICROSOFT CONFIDENTIAL
//
//-----------------------------------------------------------------------------

#include <cores\arm\include\cpu.h>
#include "AT91_GPIO.h"
#include "HAG_I2C_GPIO.h"
#include "HAG_I2C_GPIO.h"
#include "HAG_XR16l784_GPIO.h"
//--//

BOOL CPU_GPIO_Initialize()
{
	return AT91_GPIO_Driver::Initialize() && 
		   HAG_I2C_GPIO_Driver::Initialize() && 
		   HAG_XR16l784_GPIO_Driver::Initialize();
}

BOOL CPU_GPIO_Uninitialize()
{
    return AT91_GPIO_Driver::Uninitialize() && 
		   HAG_I2C_GPIO_Driver::Uninitialize() &&
		   HAG_XR16l784_GPIO_Driver::Uninitialize();
}

UINT32 CPU_GPIO_Attributes( GPIO_PIN Pin )
{
	if (Pin < AT91_MAX_GPIO)
		return AT91_GPIO_Driver::Attributes( Pin );
	else if (Pin < HAG_I2C_MAX_GPIO)
		return HAG_I2C_GPIO_Driver::Attributes( Pin - HAG_I2C_GPIO_Driver::c_FirstIo );
	else if (Pin < HAG_XR16l784_MAX_GPIO)
		return HAG_XR16l784_GPIO_Driver::Attributes( Pin - HAG_XR16l784_GPIO_Driver::c_FirstIo );
	else
		return 0;
}

void CPU_GPIO_DisablePin( GPIO_PIN Pin, GPIO_RESISTOR ResistorState, UINT32 Direction, GPIO_ALT_MODE AltFunction )
{
    if (Pin < AT91_MAX_GPIO)
		AT91_GPIO_Driver::DisablePin( Pin, ResistorState, Direction, AltFunction );
	else if (Pin < HAG_I2C_MAX_GPIO)
		HAG_I2C_GPIO_Driver::DisablePin( Pin - HAG_I2C_GPIO_Driver::c_FirstIo, ResistorState, Direction, AltFunction );
	else if (Pin < HAG_XR16l784_MAX_GPIO)
		HAG_XR16l784_GPIO_Driver::DisablePin( Pin - HAG_XR16l784_GPIO_Driver::c_FirstIo, ResistorState, Direction, AltFunction );	
}

void CPU_GPIO_EnableOutputPin( GPIO_PIN Pin, BOOL InitialState )
{
    if (Pin < AT91_MAX_GPIO)
		AT91_GPIO_Driver::EnableOutputPin( Pin, InitialState );
	else if (Pin < HAG_I2C_MAX_GPIO)
		HAG_I2C_GPIO_Driver::EnableOutputPin( Pin - HAG_I2C_GPIO_Driver::c_FirstIo, InitialState );
	else if (Pin < HAG_XR16l784_MAX_GPIO)
		HAG_XR16l784_GPIO_Driver::EnableOutputPin( Pin - HAG_XR16l784_GPIO_Driver::c_FirstIo, InitialState );
}

BOOL CPU_GPIO_EnableInputPin( GPIO_PIN Pin, BOOL GlitchFilterEnable, GPIO_INTERRUPT_SERVICE_ROUTINE ISR, GPIO_INT_EDGE IntEdge, GPIO_RESISTOR ResistorState )
{
    if (Pin < AT91_MAX_GPIO)
		return AT91_GPIO_Driver::EnableInputPin( Pin, GlitchFilterEnable, ISR, 0, IntEdge, ResistorState );
	else if (Pin < HAG_I2C_MAX_GPIO)
		return HAG_I2C_GPIO_Driver::EnableInputPin( Pin - HAG_I2C_GPIO_Driver::c_FirstIo, GlitchFilterEnable, ISR, 0, IntEdge, ResistorState );
	else if (Pin < HAG_XR16l784_MAX_GPIO)
		return HAG_XR16l784_GPIO_Driver::EnableInputPin( Pin - HAG_XR16l784_GPIO_Driver::c_FirstIo, GlitchFilterEnable, ISR, 0, IntEdge, ResistorState );
	else
		return FALSE;
}

BOOL CPU_GPIO_EnableInputPin2( GPIO_PIN Pin, BOOL GlitchFilterEnable, GPIO_INTERRUPT_SERVICE_ROUTINE ISR, void* ISR_Param, GPIO_INT_EDGE IntEdge, GPIO_RESISTOR ResistorState )
{
    if (Pin < AT91_MAX_GPIO)
		return AT91_GPIO_Driver::EnableInputPin( Pin, GlitchFilterEnable, ISR, ISR_Param, IntEdge, ResistorState );
	else if (Pin < HAG_I2C_MAX_GPIO)
		return HAG_I2C_GPIO_Driver::EnableInputPin( Pin - HAG_I2C_GPIO_Driver::c_FirstIo, GlitchFilterEnable, ISR, ISR_Param, IntEdge, ResistorState );
	else if (Pin < HAG_XR16l784_MAX_GPIO)
		return HAG_XR16l784_GPIO_Driver::EnableInputPin( Pin - HAG_XR16l784_GPIO_Driver::c_FirstIo, GlitchFilterEnable, ISR, ISR_Param, IntEdge, ResistorState );	
	else
		return FALSE;
}

BOOL CPU_GPIO_GetPinState( GPIO_PIN Pin )
{
    if (Pin < AT91_MAX_GPIO)
		return AT91_GPIO_Driver::GetPinState( Pin );
	else if (Pin < HAG_I2C_MAX_GPIO)
		return HAG_I2C_GPIO_Driver::GetPinState( Pin - HAG_I2C_GPIO_Driver::c_FirstIo );
	else if (Pin < HAG_XR16l784_MAX_GPIO)
		return HAG_XR16l784_GPIO_Driver::GetPinState( Pin - HAG_XR16l784_GPIO_Driver::c_FirstIo );	
	else
		return FALSE;
}

void CPU_GPIO_SetPinState( GPIO_PIN Pin, BOOL PinState )
{
    if (Pin < AT91_MAX_GPIO)
		AT91_GPIO_Driver::SetPinState( Pin, PinState );
	else if (Pin < HAG_I2C_MAX_GPIO)
		HAG_I2C_GPIO_Driver::SetPinState( Pin - HAG_I2C_GPIO_Driver::c_FirstIo, PinState );
	else if (Pin < HAG_XR16l784_MAX_GPIO)
		HAG_XR16l784_GPIO_Driver::SetPinState( Pin - HAG_XR16l784_GPIO_Driver::c_FirstIo, PinState );	
}

INT32 CPU_GPIO_GetPinCount()
{
    return AT91_GPIO_Driver::c_MaxPins + HAG_I2C_GPIO_Driver::c_MaxPins + HAG_XR16l784_GPIO_Driver::c_MaxPins;
}

void CPU_GPIO_GetPinsMap( UINT8* pins, size_t size )
{
    AT91_GPIO_Driver::GetPinsMap( pins, size );
	if (size > AT91_GPIO_Driver::c_MaxPins)
		HAG_I2C_GPIO_Driver::GetPinsMap( &pins[AT91_GPIO_Driver::c_MaxPins], size-AT91_GPIO_Driver::c_MaxPins );
	if (size > HAG_I2C_GPIO_Driver::c_MaxPins)
		HAG_XR16l784_GPIO_Driver::GetPinsMap( &pins[AT91_GPIO_Driver::c_MaxPins+AT91_GPIO_Driver::c_MaxPins], size-(AT91_GPIO_Driver::c_MaxPins+AT91_GPIO_Driver::c_MaxPins) );
		
}

UINT8 CPU_GPIO_GetSupportedResistorModes(GPIO_PIN pin)
{
    return AT91_GPIO_Driver::c_GPIO_ResistorMode;
}

UINT8 CPU_GPIO_GetSupportedInterruptModes(GPIO_PIN pin)
{
   return AT91_GPIO_Driver::c_GPIO_InterruptMode;
}

//--//

BOOL CPU_GPIO_PinIsBusy( GPIO_PIN Pin )
{
    if (Pin < AT91_MAX_GPIO)
		return AT91_GPIO_Driver::PinIsBusy( Pin );
	else if (Pin < HAG_I2C_MAX_GPIO)
		return HAG_I2C_GPIO_Driver::PinIsBusy( Pin - HAG_I2C_GPIO_Driver::c_FirstIo );
	else if (Pin < HAG_XR16l784_MAX_GPIO)
		return HAG_XR16l784_GPIO_Driver::PinIsBusy( Pin - HAG_XR16l784_GPIO_Driver::c_FirstIo );	
	else
		return FALSE;
}

BOOL CPU_GPIO_ReservePin( GPIO_PIN Pin, BOOL fReserve )
{
    if (Pin < AT91_MAX_GPIO)
		return AT91_GPIO_Driver::ReservePin( Pin, fReserve );
	else if (Pin < HAG_I2C_MAX_GPIO)
		return HAG_I2C_GPIO_Driver::ReservePin( Pin - HAG_I2C_GPIO_Driver::c_FirstIo, fReserve );
	else if (Pin < HAG_XR16l784_MAX_GPIO)
		return HAG_XR16l784_GPIO_Driver::ReservePin( Pin - HAG_XR16l784_GPIO_Driver::c_FirstIo, fReserve );
	else
		return FALSE;
}

UINT32 CPU_GPIO_GetDebounce()
{
    return AT91_GPIO_Driver::GetDebounce();
}

BOOL CPU_GPIO_SetDebounce( INT64 debounceTimeMilliseconds )
{
    return AT91_GPIO_Driver::SetDebounce( debounceTimeMilliseconds );
}

void I2C_IS_UP()
{
	HAG_I2C_GPIO_Driver::I2C_IS_UP();
}

